﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Data;
using System.Threading;
using System.Globalization;
public partial class reps : System.Web.UI.Page
{
    ReportDocument Rep;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["rep"] != null)
        {
            Rep = new ReportDocument();
            Rep.Load(Server.MapPath("~/" + Request.QueryString["rep"] + ".rpt"), OpenReportMethod.OpenReportByTempCopy);//ActivityReport

            switch (Request.QueryString["rep"])
            {
                case "CustomerReport":

                    Rep.SetDataSource(DB.datasetfill(Request.QueryString["id"], Request.QueryString["sid"]));
                    break;
                case "Checkings":
                    //string[] vals = Request.QueryString["params"].Split(',');
                    CheckingHistory chk = (CheckingHistory)Session["thats"];
                    Rep.SetDataSource(chk);
                    //Rep.SetParameterValue("totalreceived", vals[0]);
                    //Rep.SetParameterValue("receivedprice", vals[1]);
                    //Rep.SetParameterValue("notreceived", vals[2]);
                    //Rep.SetParameterValue("notreceivedprice", vals[3]);
                    Rep.SetParameterValue("totalreceived", "341432");
                    Rep.SetParameterValue("receivedprice", "341432");
                    Rep.SetParameterValue("notreceived", "341432");
                    Rep.SetParameterValue("notreceivedprice", "341432");
                    Rep.SetParameterValue("from", Request.QueryString["from"]);//vals[4]);
                    Rep.SetParameterValue("to", DateTime.Now.ToShortDateString());//vals[4]);
                    
                    
                    break;
                case "CompnySubs":
                    Rep.SetDataSource(DB.SubsRepfill(Request.QueryString["id"], Request.QueryString["sid"], Request.QueryString["did"]));
                    break;
                case "customerallsubs":
                    Rep.SetDataSource(DB.Customerallinfos(Request.QueryString["id"], Request.QueryString["sid"]));
                    break;
                case "SubHistoryrep":
                    Rep.SetDataSource(DB.getSubHistoryrep(Request.QueryString["id"]));



                    break;
                case "InternalHisRep":
                   
                    Rep.SetDataSource(Session["data"]);
                    //if (Request.QueryString["from"] != null)
                    //{
                    //    Rep.SetParameterValue("from", Request.QueryString["from"]);//vals[4]);
                    //    Rep.SetParameterValue("to", Request.QueryString["to"]);//vals[4]);
                    //}
                    //else
                    //{
                    //    Rep.SetParameterValue("from", DateTime.Now.ToShortDateString());//vals[4]);
                    //    Rep.SetParameterValue("to", DateTime.Now.ToShortDateString());//vals[4]);
                    //}

                    break;
                case "CustomerReceipts2":
                    CustomerReceipt cus = DB.getIntReceipt(Session["filter"].ToString(), Request.QueryString["intid"]);
                    Rep.SetDataSource(cus);
                    if (Request.QueryString["from"] != null)
                    {
                        Rep.SetParameterValue("from", Request.QueryString["from"]);//vals[4]);
                        Rep.SetParameterValue("to", Request.QueryString["to"]);//vals[4]);
                    }
                    else
                    {
                        Rep.SetParameterValue("from", DateTime.Now.ToShortDateString());//vals[4]);
                        Rep.SetParameterValue("to", DateTime.Now.ToShortDateString());//vals[4]);
                    }
                    break;
                default:
                    // Rep.SetDataSource(DB.datasetfill("0"));
                    break;

            }


            Rep.Refresh();
            CrystalReportViewer1.ReportSource = Rep;
            CrystalReportViewer1.Visible = true;
            CrystalReportViewer1.DataBind();
            CrystalReportViewer1.RefreshReport();
            if (!Page.IsPostBack)
            {
                //DataRow drow = (DataRow)Session["allsettings"];
                //if (bool.Parse(drow["saverepondisk"].ToString()))
                //{
                savings.Visible = true;
                // }

            }
        }

    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);


            }
        }

    }
  
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        DataRow drow = (DataRow)Session["allsettings"];

        Response.Buffer = false;

        Response.ClearContent();
        Response.ClearHeaders();
        //string format = drow["repformat"].ToString();
        Rep.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "myrep");
                 
        //try
        //{
        //    switch (format)
        //    {
        //        case "pdf":
        //            break;
        //        case "xls":
        //            Rep.ExportToHttpResponse(ExportFormatType.Excel, Response, true, "myrep");
        //            break;
        //        case "doc":
        //            Rep.ExportToHttpResponse(ExportFormatType.WordForWindows, Response, true, "myrep");
        //            break;
        //        default:
        //            Rep.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "myrep");
        //            break;
        //    }

        //}
        //catch (Exception ex)
        //{

        //}
        
    }
}
